/**
 * 定位
 */
import Vuex from 'vuex'
import store from '@/store';//在js文件引入store模块
import { Notify } from 'vant'
//初始化 定位
const initLocation = {
    initMap(id){
        let mapObj = new AMap.Map(id, {})
        let geolocation;
        mapObj.plugin(['AMap.Geolocation'], function () {
            geolocation = new AMap.Geolocation({
                GeoLocationFirst:true,
                noGeoLocation:0,
                enableHighAccuracy: true, //  是否使用高精度定位，默认:true
                timeout: 10000, //  超过10秒后停止定位，默认：无穷大
                maximumAge: 0, // 定位结果缓存0毫秒，默认：0
                convert: true, // 自动偏移坐标，偏移后的坐标为高德坐标，默认：true
                showButton: false, //  显示定位按钮，默认：true
                buttonPosition: 'LB',  // 定位按钮停靠位置，默认：'LB'，左下角
                buttonOffset: new AMap.Pixel(10, 20), //  定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
                showMarker: true, //  定位成功后在定位到的位置显示点标记，默认：true
                showCircle: true, //  定位成功后用圆圈表示定位精度范围，默认：true
                panToLocation: true,  //  定位成功后将定位到的位置作为地图中心点，默认：true
                zoomToAccuracy: true  //  定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
            })
            // console.log(AMap.UA.ios);
            // if (AMap.UA.ios) {
            //     //使用远程定位，见 remogeo.js
            //     let remoGeo = new RemoGeoLocation();
            //     //替换方法
            //     navigator.geolocation.getCurrentPosition = function() {
            //         return remoGeo.getCurrentPosition.apply(remoGeo, arguments);
            //     };
            //     //替换方法
            //     navigator.geolocation.watchPosition =()=> {
            //         return remoGeo.watchPosition.apply(remoGeo, arguments);
            //     };
            // }
                mapObj.addControl(geolocation)
                geolocation.getCurrentPosition();

        })
        return geolocation;
    }
}

//ip定位城市
const getIpLocation =()=>{
    AMap.plugin('AMap.CitySearch',  ()=>{
        let citySearch = new AMap.CitySearch();
        citySearch.getLocalCity( (status, result) =>{
            if (status === 'complete' && result.info === 'OK') {
                // 查询成功，result即为当前所在城市信息
                let lnglat =[result.bounds.northeast.lng,result.bounds.northeast.lat]
                Converse(lnglat,result.adcode)
            }
        })
    })

}
//根据经纬度 反向推出地理位置
const Converse =(lnglat,adcode)=>{
    AMap.plugin('AMap.Geocoder', ()=>{
        let geocoder = new AMap.Geocoder({
            adcode:adcode
        });
        geocoder.getAddress(lnglat, (status, result)=>{
            if (status === 'complete' && result.info === 'OK') {
                console.log(result)
                setLoacation(result)
            }
        })
    })
}

//提交到vuex中 setLocation
const setLoacation=(result)=>{
    if (result.regeocode){
        store.commit('Amap/setLocation',{  //Amap 表示vuex的文件名
            addressComponent:{
                adcode: result.regeocode.addressComponent.adcode,
                city:result.regeocode.addressComponent.city,
                citycode:result.regeocode.addressComponent.citycode,
                country:result.regeocode.addressComponent.country,
                district:result.regeocode.addressComponent.district,
                province:result.regeocode.addressComponent.province,
                street:result.regeocode.addressComponent.street,
                streetNumber:result.regeocode.addressComponent.streetNumber,
                township: result.regeocode.addressComponent.township,
            },
            formattedAddress:result.regeocode.formattedAddress,
        })
    }else{
        store.commit('Amap/setLocation',{  //Amap 表示vuex的文件名
            addressComponent:{
                adcode: result.addressComponent.adcode,
                city:result.addressComponent.city,
                citycode:result.addressComponent.citycode,
                country:result.addressComponent.country,
                district:result.addressComponent.district,
                province:result.addressComponent.province,
                street:result.addressComponent.street,
                streetNumber:result.addressComponent.streetNumber,
                township: result.addressComponent.township,
            },
            position:{
                Q: result.position.Q,
                R: result.position.R,
                lat: result.position.lat,
                lng: result.position.lng,
            },
            formattedAddress:result.formattedAddress,
        })
    }
}


/**获取地图定位*/
export const  getLocation = ()=> {
    let geolocation = initLocation.initMap("map-container"); //定位
    AMap.event.addListener(geolocation, "complete", result => {
        setLoacation(result);
        console.log(result)
    });
    AMap.event.addListener(geolocation, 'error', onError =>{ //返回定位出错信息
        // Notify({ type: 'danger', message: onError.message});
        //如果定位失败则使用ip定位城市
         getIpLocation();
    });
}

